package cn.hxzy.course.mapper;

import cn.hxzy.common.domain.FileInfo;
import cn.hxzy.common.domain.Section;
import cn.hxzy.common.domain.Task;
import cn.hxzy.common.domain.TaskManager;
import cn.hxzy.common.domain.vo.TaskVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
public interface TaskMapper extends BaseMapper<Task> {
   
    //视频名称模糊查询
    @Select("SELECT \n" +
            "    t.task_id,\n" +
            "    t.task_name,\n" +
            "FROM \n" +
            "    sys_course AS c\n" +
            "JOIN \n" +
            "    sys_chapter AS ch ON c.course_id = ch.course_id\n" +
            "JOIN \n" +
            "    sys_section AS s ON ch.chapter_id = s.chapter_id\n" +
            "JOIN \n" +
            "    sys_task AS t ON s.section_id = t.section_id\n" +
            "WHERE \n" +
            "    c.course_id = #{courseId}\n" +
            "    AND t.task_name LIKE '%${taskName}%'\n" +
            "    AND c.deleted = 0\n" +
            "    AND ch.deleted = 0;")
    List<Task> selectTask(String taskName,Integer courseId);

    //查此课程所有作业
    @Select("SELECT COUNT(0) FROM sys_task WHERE section_id IN(\n" +
            "SELECT section_id FROM sys_section WHERE chapter_id IN(\n" +
            "SELECT chapter_id FROM sys_chapter WHERE course_id=#{courseId}))")
    double selectCount(Long courseId);

    @Select("select * from sys_task where section_id=#{sectionId}")
    Task getList(int sectionId);

    //删除作业
    @Delete("DELETE FROM sys_task WHERE task_id=#{taskId}")
    int deleteTask(Integer taskId);


    //查询作业和小结名称
    @Select("select * from sys_task where task_id=#{taskId}")
    @Results({
            @Result(property = "section",column = "section_id",
                    javaType = Section.class,
                    one = @One(select = "cn.hxzy.course.mapper.SectionMapper.selectById")),
    })
    Map selectTaskById(Integer taskId);

    //学生上传作业
    @Insert("insert into " +
            "task_manager(student_id,task_id,submit_time,task_txt,commit) " +
            "values(#{studentId},#{taskId},#{submitTime},#{taskTxt},#{commit})")
    int insertTask(TaskManager taskManager);

    //学生修改作业
    @Update("update task_manager set task_txt=#{taskTxt} where student_id=#{studentId} AND task_id=#{taskId}")
    int updateTask(String taskTxt,Integer studentId,Integer taskId);




    //老师布置作业
    @Insert("insert into " +
            "sys_task(task_name,teacher_id,section_id,task_item) " +
            "values(#{taskName},#{teacherId},#{sectionId},#{taskItem})")
    int teacherInsertTask(Task task);


    //老师修改作业
    @Update("update sys_task " +
            "set task_name = #{taskName}" +
            "where teacher_id = #{teacherId} and section_id = #{sectionId}")
    int teacherUpdateTask(Task task);


    //根据小节id查章节名称
    @Select("SELECT chapter_name \n" +
            "FROM `sys_chapter`,`sys_section`\n" +
            "where sys_chapter.chapter_id = sys_section.chapter_id and section_id = #{id}")
    String selectSetionName(Integer id);
}
